package org.apache.james.mime4j.field.datetime.parser;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

public class SimpleCharStream
{
  public static final boolean staticFlag;
  int available;
  protected int[] bufcolumn;
  protected char[] buffer;
  protected int[] bufline;
  public int bufpos;
  int bufsize;
  protected int column;
  protected int inBuf;
  protected Reader inputStream;
  protected int line;
  protected int maxNextCharInd;
  protected boolean prevCharIsCR;
  protected boolean prevCharIsLF;
  protected int tabSize;
  int tokenBegin;

  public SimpleCharStream(InputStream paramInputStream)
  {
    this(paramInputStream, 1, 1, 4096);
  }

  public SimpleCharStream(InputStream paramInputStream, int paramInt1, int paramInt2)
  {
    this(paramInputStream, paramInt1, paramInt2, 4096);
  }

  public SimpleCharStream(InputStream paramInputStream, int paramInt1, int paramInt2, int paramInt3)
  {
    this(new InputStreamReader(paramInputStream), paramInt1, paramInt2, paramInt3);
  }

  public SimpleCharStream(InputStream paramInputStream, String paramString)
    throws UnsupportedEncodingException
  {
    this(paramInputStream, paramString, 1, 1, 4096);
  }

  public SimpleCharStream(InputStream paramInputStream, String paramString, int paramInt1, int paramInt2)
    throws UnsupportedEncodingException
  {
    this(paramInputStream, paramString, paramInt1, paramInt2, 4096);
  }

  public SimpleCharStream(Reader paramReader)
  {
    this(paramReader, 1, 1, 4096);
  }

  public SimpleCharStream(Reader paramReader, int paramInt1, int paramInt2)
  {
    this(paramReader, paramInt1, paramInt2, 4096);
  }

  public SimpleCharStream(Reader paramReader, int paramInt1, int paramInt2, int paramInt3)
  {
    this.bufpos = -1;
    this.column = 0;
    this.line = 1;
    this.prevCharIsCR = false;
    this.prevCharIsLF = false;
    this.maxNextCharInd = 0;
    this.inBuf = 0;
    this.tabSize = 8;
    this.inputStream = paramReader;
    this.line = paramInt1;
    this.column = (paramInt2 - 1);
    this.bufsize = paramInt3;
    this.available = paramInt3;
    this.buffer = new char[paramInt3];
    this.bufline = new int[paramInt3];
    this.bufcolumn = new int[paramInt3];
  }

  public char BeginToken()
    throws IOException
  {
    this.tokenBegin = -1;
    int i = readChar();
    this.tokenBegin = this.bufpos;
    return i;
  }

  public void Done()
  {
    this.buffer = null;
    this.bufline = null;
    this.bufcolumn = null;
  }

  protected void ExpandBuff(boolean paramBoolean)
  {
    char[] arrayOfChar = new char[2048 + this.bufsize];
    int[] arrayOfInt1 = new int[2048 + this.bufsize];
    int[] arrayOfInt2 = new int[2048 + this.bufsize];
    if (paramBoolean);
    try
    {
      System.arraycopy(this.buffer, this.tokenBegin, arrayOfChar, 0, this.bufsize - this.tokenBegin);
      System.arraycopy(this.buffer, 0, arrayOfChar, this.bufsize - this.tokenBegin, this.bufpos);
      this.buffer = arrayOfChar;
      System.arraycopy(this.bufline, this.tokenBegin, arrayOfInt1, 0, this.bufsize - this.tokenBegin);
      System.arraycopy(this.bufline, 0, arrayOfInt1, this.bufsize - this.tokenBegin, this.bufpos);
      this.bufline = arrayOfInt1;
      System.arraycopy(this.bufcolumn, this.tokenBegin, arrayOfInt2, 0, this.bufsize - this.tokenBegin);
      System.arraycopy(this.bufcolumn, 0, arrayOfInt2, this.bufsize - this.tokenBegin, this.bufpos);
      this.bufcolumn = arrayOfInt2;
      int j = this.bufpos + this.bufsize - this.tokenBegin;
      this.bufpos = j;
      this.maxNextCharInd = j;
      this.bufsize = (2048 + this.bufsize);
      this.available = this.bufsize;
      this.tokenBegin = 0;
      return;
      System.arraycopy(this.buffer, this.tokenBegin, arrayOfChar, 0, this.bufsize - this.tokenBegin);
      this.buffer = arrayOfChar;
      System.arraycopy(this.bufline, this.tokenBegin, arrayOfInt1, 0, this.bufsize - this.tokenBegin);
      this.bufline = arrayOfInt1;
      System.arraycopy(this.bufcolumn, this.tokenBegin, arrayOfInt2, 0, this.bufsize - this.tokenBegin);
      this.bufcolumn = arrayOfInt2;
      int i = this.bufpos - this.tokenBegin;
      this.bufpos = i;
      this.maxNextCharInd = i;
    }
    catch (Throwable localThrowable)
    {
      throw new Error(localThrowable.getMessage());
    }
  }

  protected void FillBuff()
    throws IOException
  {
    int i;
    if (this.maxNextCharInd == this.available)
    {
      if (this.available != this.bufsize)
        break label159;
      if (this.tokenBegin <= 2048)
        break label131;
      this.maxNextCharInd = 0;
      this.bufpos = 0;
      this.available = this.tokenBegin;
    }
    try
    {
      i = this.inputStream.read(this.buffer, this.maxNextCharInd, this.available - this.maxNextCharInd);
      if (i == -1)
      {
        this.inputStream.close();
        label131: label159: throw new IOException();
      }
    }
    catch (IOException localIOException)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                this.bufpos -= 1;
                backup(0);
                if (this.tokenBegin == -1)
                  this.tokenBegin = this.bufpos;
                throw localIOException;
                if (this.tokenBegin >= 0)
                  break;
                this.maxNextCharInd = 0;
                this.bufpos = 0;
              }
              ExpandBuff(false);
            }
            if (this.available <= this.tokenBegin)
              break;
            this.available = this.bufsize;
          }
          if (this.tokenBegin - this.available >= 2048)
            break;
          ExpandBuff(true);
        }
        this.available = this.tokenBegin;
      }
      this.maxNextCharInd = (i + this.maxNextCharInd);
    }
  }

  public String GetImage()
  {
    String str;
    if (this.bufpos >= this.tokenBegin)
      str = new String(this.buffer, this.tokenBegin, 1 + this.bufpos - this.tokenBegin);
    while (true)
    {
      return str;
      str = new String(this.buffer, this.tokenBegin, this.bufsize - this.tokenBegin) + new String(this.buffer, 0, 1 + this.bufpos);
    }
  }

  public char[] GetSuffix(int paramInt)
  {
    char[] arrayOfChar = new char[paramInt];
    if (1 + this.bufpos >= paramInt)
      System.arraycopy(this.buffer, 1 + this.bufpos - paramInt, arrayOfChar, 0, paramInt);
    while (true)
    {
      return arrayOfChar;
      System.arraycopy(this.buffer, this.bufsize - paramInt - this.bufpos - 1, arrayOfChar, 0, paramInt - this.bufpos - 1);
      System.arraycopy(this.buffer, 0, arrayOfChar, paramInt - this.bufpos - 1, 1 + this.bufpos);
    }
  }

  public void ReInit(InputStream paramInputStream)
  {
    ReInit(paramInputStream, 1, 1, 4096);
  }

  public void ReInit(InputStream paramInputStream, int paramInt1, int paramInt2)
  {
    ReInit(paramInputStream, paramInt1, paramInt2, 4096);
  }

  public void ReInit(InputStream paramInputStream, int paramInt1, int paramInt2, int paramInt3)
  {
    ReInit(new InputStreamReader(paramInputStream), paramInt1, paramInt2, paramInt3);
  }

  public void ReInit(InputStream paramInputStream, String paramString)
    throws UnsupportedEncodingException
  {
    ReInit(paramInputStream, paramString, 1, 1, 4096);
  }

  public void ReInit(InputStream paramInputStream, String paramString, int paramInt1, int paramInt2)
    throws UnsupportedEncodingException
  {
    ReInit(paramInputStream, paramString, paramInt1, paramInt2, 4096);
  }

  public void ReInit(InputStream paramInputStream, String paramString, int paramInt1, int paramInt2, int paramInt3)
    throws UnsupportedEncodingException
  {
    InputStreamReader localInputStreamReader;
    if (paramString == null)
      localInputStreamReader = new InputStreamReader(paramInputStream);
    while (true)
    {
      ReInit(localInputStreamReader, paramInt1, paramInt2, paramInt3);
      return;
      localInputStreamReader = new InputStreamReader(paramInputStream, paramString);
    }
  }

  public void ReInit(Reader paramReader)
  {
    ReInit(paramReader, 1, 1, 4096);
  }

  public void ReInit(Reader paramReader, int paramInt1, int paramInt2)
  {
    ReInit(paramReader, paramInt1, paramInt2, 4096);
  }

  public void ReInit(Reader paramReader, int paramInt1, int paramInt2, int paramInt3)
  {
    this.inputStream = paramReader;
    this.line = paramInt1;
    this.column = (paramInt2 - 1);
    if ((this.buffer == null) || (paramInt3 != this.buffer.length))
    {
      this.bufsize = paramInt3;
      this.available = paramInt3;
      this.buffer = new char[paramInt3];
      this.bufline = new int[paramInt3];
      this.bufcolumn = new int[paramInt3];
    }
    this.prevCharIsCR = false;
    this.prevCharIsLF = false;
    this.maxNextCharInd = 0;
    this.inBuf = 0;
    this.tokenBegin = 0;
    this.bufpos = -1;
  }

  protected void UpdateLineColumn(char paramChar)
  {
    this.column = (1 + this.column);
    if (this.prevCharIsLF)
    {
      this.prevCharIsLF = false;
      int j = this.line;
      this.column = 1;
      this.line = (j + 1);
      switch (paramChar)
      {
      case '\11':
      case '\f':
      default:
      case '\r':
      case '\n':
      case '\t':
      }
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              do
              {
                this.bufline[this.bufpos] = this.line;
                this.bufcolumn[this.bufpos] = this.column;
                return;
              }
              while (!(this.prevCharIsCR));
              this.prevCharIsCR = false;
              if (paramChar != '\n')
                break;
              this.prevCharIsLF = true;
            }
            int i = this.line;
            this.column = 1;
            this.line = (i + 1);
          }
          this.prevCharIsCR = true;
        }
        this.prevCharIsLF = true;
      }
      this.column -= 1;
      this.column += this.tabSize - this.column % this.tabSize;
    }
  }

  public void adjustBeginLineColumn(int paramInt1, int paramInt2)
  {
    int j;
    int k;
    int l;
    int i1;
    int i2;
    int i = this.tokenBegin;
    if (this.bufpos >= this.tokenBegin)
      j = 1 + this.bufpos - this.tokenBegin + this.inBuf;
    while (true)
    {
      k = 0;
      l = 0;
      i1 = 0;
      while (true)
      {
        if (k >= j)
          break label173;
        int[] arrayOfInt5 = this.bufline;
        l = i % this.bufsize;
        int i7 = arrayOfInt5[l];
        int[] arrayOfInt6 = this.bufline;
        int i8 = ++i % this.bufsize;
        if (i7 != arrayOfInt6[i8])
          break label173;
        this.bufline[l] = paramInt1;
        int i9 = i1 + this.bufcolumn[i8] - this.bufcolumn[l];
        this.bufcolumn[l] = (paramInt2 + i1);
        i1 = i9;
        ++k;
      }
      j = 1 + this.bufsize - this.tokenBegin + this.bufpos + this.inBuf;
    }
    if (k < j)
    {
      label173: int[] arrayOfInt1 = this.bufline;
      i2 = paramInt1 + 1;
      arrayOfInt1[l] = paramInt1;
      this.bufcolumn[l] = (paramInt2 + i1);
      int i3 = k;
      while (true)
      {
        int i4;
        while (true)
        {
          i4 = i3 + 1;
          if (i3 >= j)
            break label315;
          int[] arrayOfInt2 = this.bufline;
          l = i % this.bufsize;
          int i5 = arrayOfInt2[l];
          int[] arrayOfInt3 = this.bufline;
          if (i5 == ???[(++i % this.bufsize)])
            break;
          int[] arrayOfInt4 = this.bufline;
          int i6 = i2 + 1;
          arrayOfInt4[l] = i2;
          i3 = i4;
          i2 = i6;
        }
        this.bufline[l] = i2;
        i3 = i4;
      }
    }
    label315: this.line = this.bufline[l];
    this.column = this.bufcolumn[l];
  }

  public void backup(int paramInt)
  {
    this.inBuf = (paramInt + this.inBuf);
    int i = this.bufpos - paramInt;
    this.bufpos = i;
    if (i < 0)
      this.bufpos += this.bufsize;
  }

  public int getBeginColumn()
  {
    return this.bufcolumn[this.tokenBegin];
  }

  public int getBeginLine()
  {
    return this.bufline[this.tokenBegin];
  }

  public int getColumn()
  {
    return this.bufcolumn[this.bufpos];
  }

  public int getEndColumn()
  {
    return this.bufcolumn[this.bufpos];
  }

  public int getEndLine()
  {
    return this.bufline[this.bufpos];
  }

  public int getLine()
  {
    return this.bufline[this.bufpos];
  }

  protected int getTabSize(int paramInt)
  {
    return this.tabSize;
  }

  public char readChar()
    throws IOException
  {
    int j;
    if (this.inBuf > 0)
    {
      this.inBuf -= 1;
      int k = 1 + this.bufpos;
      this.bufpos = k;
      if (k == this.bufsize)
        this.bufpos = 0;
      j = this.buffer[this.bufpos];
    }
    while (true)
    {
      return j;
      int i = 1 + this.bufpos;
      this.bufpos = i;
      if (i >= this.maxNextCharInd)
        FillBuff();
      char c = this.buffer[this.bufpos];
      UpdateLineColumn(c);
      j = c;
    }
  }

  protected void setTabSize(int paramInt)
  {
    this.tabSize = paramInt;
  }
}